package com.demo.security.cloud.service.impl;

import com.demo.security.cloud.entity.dto.UserDto;
import com.demo.security.cloud.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.Map;

@Service("authenticationService")
public class AuthenticationServiceImpl implements AuthenticationService {

    private final Map<String, UserDto> userMap;

    public AuthenticationServiceImpl(@Autowired Map<String, UserDto> userMap) {
        this.userMap = userMap;
    }

    @Override
    public UserDto authentication(String name, String password) {

        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)){
            throw new RuntimeException("账号密码为空");
        }

        UserDto userDto = getUserDto(name);
        if (userDto == null){
            throw new RuntimeException("查询不到用户");
        }
        if (!userDto.getPassword().equals(password)){
            throw new RuntimeException("查询不到用户");
        }

        return userDto;
    }

    private UserDto getUserDto(String name){

        return userMap.get(name);
    }
}
